<?php

namespace app\admin\controller\system;

use app\admin\controller\Controller;
use app\common\controller\ResourceController;
use app\common\facade\auth\Auth;
use app\model\AdminRolePermission;
use think\App;

class AdminPermission extends Controller
{

    use ResourceController;


    public function __construct(App $app)
    {
        parent::__construct($app);

        return $this->model = new \app\model\AdminPermission();
    }

    /**
     * 获取树
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function tree()
    {
        if ($msg =dmjValidate(['role_id' =>'number'],[],input())){
            return fail($msg);
        }
        $role_id = input('role_id');
        $ids = [];
        if ($role_id){
            $ids = AdminRolePermission::query()
                ->where('role_id',input('role_id'))
                ->column('permission_id');
        }


        $datas = \app\model\AdminPermission::query()->select()->toArray();

        foreach ($datas as &$item){
            if (in_array($item['id'],$ids)){
                $item['select'] = 1;
            }else{
                $item['select'] = 0;
            }
        }


        return success('', getTree($datas));

    }


    /**
     * 获取权限菜单
     */
    public function menu(){

        $data = Auth::permissions();

        $menusPermission = collect($data)->where('type',\app\model\AdminPermission::TYPE_MENU);

        return success('',getTree($menusPermission));

    }




}